/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktika;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class Frame extends JFrame {

    private final JPanel panel1 = new JPanel();
    private final JPanel panel2 = new JPanel();
    private final JPanel panel3 = new JPanel();
    public static JTextField[][] areas1;
    public static JTextField[][] areas2;
    public static JTextField[][] areas3;
    public static Frame frame;
    public static long latency = 0;

    public static void main(String args[]) {
        frame = new Frame();
    }

    private Frame() {
        Main.size = Integer.parseInt(JOptionPane.showInputDialog("Введите размер матрицы"));
        this.setSize(800, 800);
        frameDisplayCenter(800, 800, this);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        setBar();
        panel_prop();
        this.add(panel1);
        this.add(panel2);
        this.add(panel3);
        add_buttons();
        this.setVisible(true);

    }

    private void setBar() {
        JMenuBar menuBar = new JMenuBar();

        JMenu filemenu = new JMenu("Файл");
        menuBar.add(filemenu);
        JMenuItem open = new JMenuItem("Открыть");
        filemenu.add(open);
        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileopen = new JFileChooser();
                int ret = fileopen.showDialog(null, "Открыть файл");
                if (ret == JFileChooser.APPROVE_OPTION) {
                    File file = fileopen.getSelectedFile();
                    try {
                        Scanner in = new Scanner(file);
                        for (int i = 0; i < Main.size; i++)
                        for (int j = 0; j < Main.size; j++) {
                            String temp;
                            temp = in.next();
                            areas1[i][j].setText(temp);
                        }
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
        JMenuItem changesize = new JMenuItem("Изменить размер");
        filemenu.add(changesize);
        changesize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                main(new String[]{""});
            }
        });
        JMenuItem save = new JMenuItem("Сохранить как");
        filemenu.add(save);
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileopen = new JFileChooser();
                int ret = fileopen.showDialog(null, "Сохранить");
                if (ret == JFileChooser.APPROVE_OPTION) {
                    File file = fileopen.getSelectedFile();
                    try {
                        FileWriter out = new FileWriter(file);
                        for (int i = 0; i < Main.size; i++)
                            for (int j = 0; j < Main.size; j++) {
                                out.write(areas3[i][j].getText() + "  ");
                                if(j == Main.size-1) out.write("\r\n");
                                out.flush();
                            }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
        filemenu.addSeparator();
        JMenuItem exit = new JMenuItem("Вывход");
        filemenu.add(exit);
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        JMenu help = new JMenu("Настройки");
        menuBar.add(help);
        JMenuItem latency = new JMenuItem("Настройка задержки");
        help.add(latency);
        help.addSeparator();
        latency.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Frame.latency = Integer.parseInt(JOptionPane.showInputDialog("Введите задержку между шагами в мс"));
            }
        });

        JMenuItem about = new JMenuItem("О программе");
        help.add(about);
        about.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame about = new JFrame("О программе");
                about.setSize(400,250);

                JTextArea area = new JTextArea();
                area.setEditable(false);
                area.setText("Программа для работы с матрицами\n\n\n\nВыполнили:\nКнязева В.\nКирьякова Е.\nКутуева И.\n\n\n Группа 1303\n\n\n Санкт-Петербург, 2013");
                area.setSize(400,250);
                about.add(area);
                frameDisplayCenter(400,250,about);
                about.setVisible(true);


            }
        });

        this.setJMenuBar(menuBar);
    }

    private void add_buttons() {
        JButton mult = new JButton("Умножить");
        mult.setBounds(100, 500, 100, 50);
        mult.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Main.enter();
                Main.multplication();
                Main.print(Main.result);
            }
        });
        this.add(mult);
        JButton addition = new JButton("Сложить");
        addition.setBounds(220, 500, 100, 50);
        addition.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Main.enter();
                Main.addiction();
                Main.print(Main.result);

            }
        });
        this.add(addition);
        JButton determinant1 = new JButton("Опред. 1-й");
        determinant1.setBounds(340, 500, 100, 50);
        determinant1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Детерминант 1-й матрицы равен " + Main.determinant(Main.mat1));
            }
        });
        this.add(determinant1);
        JButton determinant2 = new JButton("Опред. 2-й");
        determinant2.setBounds(460, 500, 100, 50);
        determinant2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Детерминант 2-й матрицы равен " + Main.determinant(Main.mat2));
            }
        });
        this.add(determinant2);
        JButton determinant3 = new JButton("Опред. результ");
        determinant3.setBounds(580, 500, 100, 50);
        determinant3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Детерминант результата матрицы равен " + Main.determinant(Main.result));
            }
        });
        this.add(determinant3);
        JButton random = new JButton("Случайные числа");
        random.setBounds(30, 400, 100, 50);
        random.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Main.randomfill();

            }
        });
        this.add(random);
    }

    void panel_prop() {
        panel1.setBounds(10, 10, 350, 220);
        panel2.setBounds(400, 10, 350, 220);
        panel3.setBounds(220, 270, 350, 220);
        panel1.setLayout(new GridLayout(Main.size, Main.size, 10, 10));
        panel2.setLayout(new GridLayout(Main.size, Main.size, 10, 10));
        panel3.setLayout(new GridLayout(Main.size, Main.size, 10, 10));
        areas1 = new JTextField[Main.size][Main.size];
        areas2 = new JTextField[Main.size][Main.size];
        areas3 = new JTextField[Main.size][Main.size];
        for (int i = 0; i < Main.size; i++)
            for (int j = 0; j < Main.size; j++) {
                areas1[i][j] = new JTextField();
                areas2[i][j] = new JTextField();
                areas3[i][j] = new JTextField();
                areas3[i][j].setEditable(false);
                panel1.add(areas1[i][j]);
                panel2.add(areas2[i][j]);
                panel3.add(areas3[i][j]);
            }
    }

    private static void frameDisplayCenter(int sizeWidth, int sizeHeight, JFrame frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int locationX = (screenSize.width - sizeWidth) / 2;
        int locationY = (screenSize.height - 600) / 2;
        frame.setBounds(locationX, locationY, sizeWidth, 600);
    }
}